package com.mx.tool.util;

import cn.hutool.log.Log;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: 徐建鹏.
 * @create: 2021-11-19 15:39
 * @Description:
 */
public class WebSourceDownloadUtil {

    private static final OkHttpClient client = new OkHttpClient();
    private static final Pattern srcPattern = Pattern.compile("src *= *['\"]*(\\S+)[\"']", Pattern.CASE_INSENSITIVE); // 不区分大小写
    private static final Pattern hrefPattern = Pattern.compile("href *= *['\"]*(\\S+)[\"']", Pattern.CASE_INSENSITIVE); // 不区分大小写


    public static void downloadHtmlSources(String url, String dir) throws Exception {

        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) {
            Log.get(WebSourceDownloadUtil.class).error("首页抓取失败,原因:" + response);
        }
        String indexHtml = Objects.requireNonNull(response.body()).string();
        if (!dir.endsWith("/")) {
            dir = dir + "/";
        }
        new File(dir).mkdirs();
        File indexHtmlFile = new File(dir + "index.html");
        FileUtils.writeStringToFile(indexHtmlFile, indexHtml, Charset.defaultCharset());
        Matcher srcMatcher = srcPattern.matcher(indexHtml);
        while (srcMatcher.find()) {
            saveSourcesFile(url, srcMatcher.group(1), dir);
        }
        Matcher hrefMatcher = hrefPattern.matcher(indexHtml);
        while (hrefMatcher.find()) {
            String str1 = hrefMatcher.group(1); // 捕获的子序列
            saveSourcesFile(url, str1, dir);
        }
    }

    private static void saveSourcesFile(String url, String sourcesUrl, String dir) throws Exception {
        if (!sourcesUrl.startsWith("http")) {
            String[] urls = url.split("/");
            String[] sourceUrls = sourcesUrl.split("\\.\\./");
            File sourcesIndexPathFile = new File(dir);
            if (sourceUrls.length > 1) {
                for (int i = 0; i < sourceUrls.length - 1; i++) {
                    sourcesIndexPathFile = sourcesIndexPathFile.getParentFile();
                }
                url = StringUtils.join(urls, "/", 0, urls.length - sourceUrls.length);
            }
            String sourceFileStr = sourceUrls[sourceUrls.length - 1];
            Request request = new Request.Builder().url(url + "/" + sourceFileStr).build();
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) {
                Log.get(WebSourceDownloadUtil.class).error(sourceFileStr + "抓取失败,原因:" + response);
            }
            //image/png;charset=UTF-8
            //text/html;charset=utf-8
            String contentType = response.header("Content-Type");
            if (StringUtils.isNotBlank(contentType)) {
                String type;
                if(contentType.contains(";")){
                    type = contentType.substring(contentType.indexOf("/") + 1, contentType.indexOf(";"));
                }else{
                    type = contentType.substring(contentType.indexOf("/") + 1);
                }
                if (!sourceFileStr.contains("." + type)) {
                    sourceFileStr = sourceFileStr + "." + type;
                }
            }
            File sourcesFile = new File(sourcesIndexPathFile, "/" + sourceFileStr);
//            FileUtils.touch(sourcesFile);
            FileUtils.writeByteArrayToFile(sourcesFile, Objects.requireNonNull(response.body()).bytes());
        }
    }


    public static void main(String[] args) {
        String contentType = "text/html;charset=utf-8";
        String type = contentType.substring(contentType.indexOf("/") + 1, contentType.indexOf(";"));
        System.out.println(type);
    }
}
